//
//  SandBoxDemo.m
//  往沙盒中存数据和取数据Demo
//
//  Created by 向祖华 on 16/3/31.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "SandBoxDemo.h"

@implementation SandBoxDemo

//Doc存数据
+(BOOL)storeDataWithString:(NSString*)str key:(NSString*)key{
    //获取documents文件夹的路径
    NSString * filePathDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //    NSLog(@"%@",filePathDocuments);
    //在沙盒的documents文件夹中创建某格式的文件，并得到路径
    NSString * filePath = [filePathDocuments stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",key]];
    //第二种写法
    //    NSString * filePath = [filePathDocuments stringByAppendingString:@"/%@.txt",key];
    //把数据写入txt文件,并接收存储是否成功
    BOOL result =[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    return result;
}

//Doc取数据
+(NSString *)takeDataWithKey:(NSString*)key{
    //获取documents文件夹的路径
    NSString * filePathDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //    NSLog(@"%@",filePathDocuments);
    //在沙盒的documents文件夹中对应文件的路径
    NSString * filePath = [filePathDocuments stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",key]];
    //取数据
    NSString * str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    return str;
}
//Doc删除数据
+(BOOL)deletDataWithKey:(NSString*)key{
    //获取documents文件夹的路径
    NSString * filePathDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //    NSLog(@"%@",filePathDocuments);
    //在沙盒的documents文件夹中对应文件的路径
    NSString * filePath = [filePathDocuments stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",key]];
    //创建文件管理者对象
    NSFileManager * fileManager = [NSFileManager defaultManager];
    //判断沙盒中是否有这个文件
    BOOL result = NO;
    if ([fileManager fileExistsAtPath:filePath]) {
        //删除文件
        result = [fileManager removeItemAtPath:filePath error:nil];
    } else {
        NSLog(@"沙盒中没有找到对应的文件");
    }
    return  result;
}

//temp中存数据
+(BOOL)tempStoreDataWithArray:(NSArray*)arr Key:(NSString*)key{
    NSString * filePath =[NSTemporaryDirectory() stringByAppendingString:[NSString stringWithFormat:@"%@.plist",key]];
    BOOL result =[arr writeToFile:filePath atomically:YES];
    return result;
}
//temp中取数据
+(NSArray *)tempTakeDataWithKey:(NSString*)key{
    NSString * filePath = [NSTemporaryDirectory() stringByAppendingString:[NSString stringWithFormat:@"%@.plist",key]];
    NSArray * arr =[NSArray arrayWithContentsOfFile:filePath];
    return arr;
}
//temp中删除数据
+(BOOL)tempDeletDataWithKey:(NSString*)key{
    NSString*filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",key]];
    //创建文件管理者
    NSFileManager * fileManager = [NSFileManager defaultManager];
    //判断沙盒中是否有这个文件
    BOOL result = NO;
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        //删除文件
        result = [fileManager removeItemAtPath:filePath error:nil];
    } else {
        NSLog(@"沙盒中没有找到对应的文件");
    }
    return result;
}

//Cashes中存数据
+(BOOL)cachesStoreDataWithImage:(UIImage*)image key:(NSString*)key{
    //获取cashes文件夹的路径
    NSString*cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    //在caches文件夹中创建.png文件，并获取路径
    NSString * filePath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",key]];
    //第二种写法
    //    NSString*filePath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",key]];
    //将图片png/jpeg处理为NSData对象
    NSData * data = UIImagePNGRepresentation(image);
    //将数据写入到沙盒
    BOOL result = [data writeToFile:filePath atomically:YES];
    return result;
    
}
//Cashes中取数据
+(UIImage*)cachesTakeDataWithKey:(NSString*)key{
    //获取cashes文件夹中对应名字文件的路径
    NSString*cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString * filePath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",key]];
    UIImage * image = [UIImage imageWithContentsOfFile:filePath];
    return image;
}
//Cashes中删除数据
+(BOOL)cachesDeleteDataWithKey:(NSString*)key{
    //获取cashes文件夹中对应名字文件的路径
    NSString*cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString * filePath = [cachesPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",key]];
    //创建文件管理者对象
    NSFileManager * fileManage = [NSFileManager defaultManager];
    //判断是否有对应的文件
    BOOL result = NO;
    if ([fileManage fileExistsAtPath:filePath]) {
        result = [fileManage removeItemAtPath:filePath error:nil];
    } else {
        NSLog(@"没有找到对象");
    }
    return result;
}
#pragma mark --文件夹
//在temp中创建文件夹
+(BOOL)creatFileWithKey:(NSString*)key{
    NSFileManager * filemanager = [NSFileManager defaultManager];
    //获取在temp中创建一个文件夹的路径
    NSString * filePath = [NSTemporaryDirectory() stringByAppendingString:key];
    NSLog(@"%@",filePath);
    //创建文件夹
    BOOL result = [filemanager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    return result;
}

//往temp文件夹中写入简单数据
+(BOOL)writDataWithString:(NSString*)str key:(NSString*)key{
    //获取在temp中创建一个文件夹的路径
    NSString * filePath = [NSTemporaryDirectory() stringByAppendingString:key];
    //获取在所创建的文件夹中创建一个txt文件的路径
    NSString * filePathTxt = [filePath stringByAppendingString:[NSString stringWithFormat:@"/%@.txt",key]];
    //写数据
    BOOL result = [str writeToFile:filePathTxt atomically:YES encoding:NSUTF8StringEncoding error:nil];
    return result;
}
//把文件夹移到caches中
+(BOOL)moveFileWithKey:(NSString*)key{
    //得到源文件的路径
    NSString * sourcePath = [NSTemporaryDirectory() stringByAppendingString:key];
    //得到目标文件夹得路径
    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:key];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    //移动文件
    BOOL result = [fileManager moveItemAtPath:sourcePath toPath:filePath error:nil];
    return result;
}
//删除temp文件夹中的文件夹
+(BOOL)deleteFileWithKey:(NSString*)key{
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString * sourcePath = [NSTemporaryDirectory() stringByAppendingString:key];
    //删除之前的文件
    BOOL result = NO;
    if ([fileManager fileExistsAtPath:sourcePath]) {
        result = [fileManager removeItemAtPath:sourcePath error:nil];
    }else{
        NSLog(@"没有找到要删除的对象");
    }
    return result;
}
//复制文件夹到doc文件夹中
+(BOOL)copyFileWithKey:(NSString *)key{
    //得到源文件夹的路径
    NSString * sourcePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:key];
    //得到目标文件夹得路径
    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:key];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    //复制文件夹
    BOOL result = [fileManager copyItemAtPath:sourcePath toPath:filePath error:nil];
    return result;
}

//归档
+(BOOL)encodeWithData:(Person*)person key:(NSString*)key{
    NSMutableData * data = [NSMutableData data];
    //创建NSKeyedArchiver对象，归档NSData对象
    NSKeyedArchiver*archiver =[[ NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //归档
    [archiver encodeObject:person forKey:key];
    //结束归档---一定要写
    [archiver finishEncoding];
    //存入沙盒
    NSString * filePath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:key];
    NSLog(@"归档地址%@",filePath);
    BOOL result = [data writeToFile:filePath atomically:YES];
    
    return result;
}
//反归档
+(Person*)decodeWithKey:(NSString*)key{
    //获取文件地址
    NSString*filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:key];
    //用NSData对象取到存在某个位置的文件对象
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    //反归档
    NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    //对应到关键字解码
    Person * person = [unArchiver decodeObjectForKey:key];
    //结束归档
    [unArchiver finishDecoding];
    return person;
}
@end
